home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format CD 49 / Amiga Format CD49 (2000-01-17)(Future Publishing)(GB)(Track 1 of 3)[!][issue 2000-02].iso / -serious- / misc / mmulib / c_sources / mufastchip.c < prev    next >
C/C++ Source or Header  |  1999-11-29  |  14KB  |  359 lines

  1. /*****************************************************************
  2.  ** MuFastChip                                                  **
  3.  **                                                             **
  4.  ** Enable imprecise/nonserial flags for Chip memory            **
  5.  ** Release 40.1, © 1999 THOR-Software inc.                     **
  6.  ** 9.8.1999 Thomas Richter                                     **
  7.  *****************************************************************/
  8.  
  9. /// Includes
  10. #include <exec/types.h>
  11. #include <exec/memory.h>
  12. #include <exec/ports.h>
  13. #include <exec/execbase.h>
  14. #include <dos/dos.h>
  15. #include <mmu/mmubase.h>
  16. #include <mmu/context.h>
  17. #include <mmu/mmutags.h>
  18. #include <workbench/startup.h>
  19.  
  20. #include <thor/conversions.h>
  21.  
  22. #include <proto/exec.h>
  23. #include <proto/mmu.h>
  24. #include <proto/dos.h>
  25. #include <proto/icon.h>
  26. #include <string.h>
  27. ///
  28. /// Defines
  29. #define STRINGDATE "4.9.99"
  30. #define STRINGVERSION "40.1"
  31. #define TEMPLATE "ON/S,OFF/S"
  32.  
  33. #define OPT_ON 0
  34. #define OPT_OFF 1
  35. #define OPT_WINDOW 2
  36. #define OPT_COUNT 3
  37. ///
  38. /// Statics
  39. struct MMUBase *MMUBase;
  40. struct DosLibrary *DOSBase;
  41. struct ExecBase *SysBase;
  42. struct Library *IconBase;
  43. ///
  44. /// Protos
  45. int __asm __saveds main(void);
  46. int SetCacheMode(ULONG from,ULONG size,ULONG flags,ULONG mask);
  47. struct RDArgs *ReadTTArgs(struct WBStartup *msg,LONG args[],struct RDArgs **tmp);
  48. int SetCache(struct MMUContext *ctx,ULONG from,ULONG size,ULONG flags,ULONG mask);
  49. int DisableFastChip(void);
  50. int EnableFastChip(void);
  51. BOOL FindChipLimits(ULONG *from,ULONG *size);
  52. ///
  53.  
  54. char version[]="$VER: MuFastChip " STRINGVERSION " (" STRINGDATE ") © THOR";
  55.  
  56. /// main
  57. int __asm __saveds main(void)
  58. {
  59. LONG args[OPT_COUNT];
  60. struct RDArgs *rd,*myrd;
  61. struct Process *proc;
  62. int rc=20;
  63. LONG err;
  64. struct WBStartup *msg;
  65. BPTR oldout;
  66. struct MsgPort *oldconsole;
  67.  
  68.         SysBase=*((struct ExecBase **)(4L));
  69.  
  70.         memset(args,0,sizeof(LONG)*OPT_COUNT);
  71.  
  72.         /* Wait for the workbench startup, if any */
  73.         proc=(struct Process *)FindTask(NULL);
  74.  
  75.         if (!(proc->pr_CLI)) {
  76.                 WaitPort(&(proc->pr_MsgPort));
  77.                 msg=(struct WBStartup *)GetMsg(&(proc->pr_MsgPort));
  78.         } else  msg=NULL;
  79.  
  80.         if (DOSBase=(struct DosLibrary *)OpenLibrary("dos.library",37L)) {
  81.                 if (MMUBase=(struct MMUBase *)OpenLibrary("mmu.library",0L)) {
  82.  
  83.                         err=ERROR_REQUIRED_ARG_MISSING;
  84.  
  85.                         myrd=NULL;      /* reset the temporary ReadArgs */
  86.                         oldout=NULL;
  87.                         oldconsole=NULL;
  88.                         if (msg) {
  89.                                 oldout=SelectOutput(NULL);
  90.                                 oldconsole=SetConsoleTask(NULL);
  91.                                 rd=ReadTTArgs(msg,args,&myrd);
  92.                         } else  rd=ReadArgs(TEMPLATE,args,NULL);
  93.  
  94.                         if (rd) {
  95.                                 if (!GetMMUType()) {
  96.                                         Printf("MuFastChip requires a working MMU.\n");
  97.                                         err=10;
  98.                                 } else {
  99.  
  100.                                         err=0;
  101.  
  102.                                         if (args[OPT_ON] || (!args[OPT_OFF])) {
  103.                                                 err=EnableFastChip();
  104.                                         }
  105.  
  106.                                         if (args[OPT_OFF]) {
  107.                                                 err=DisableFastChip();
  108.                                         }
  109.                                 }
  110.  
  111.                                 FreeArgs(rd);
  112.                                 if (myrd) FreeDosObject(DOS_RDARGS,myrd);
  113.                                 if (msg)  Close(SelectOutput(NULL));
  114.                         } else  err=IoErr();
  115.  
  116.                         if (msg) {
  117.                                 SelectOutput(oldout);
  118.                                 SetConsoleTask(oldconsole);
  119.                         }
  120.  
  121.                         if (err<64) {
  122.                                 rc=err;
  123.                                 err=0;
  124.                         } else {
  125.                                 if (!msg) PrintFault(err,"MuFastChip failed");
  126.                                 rc=10;
  127.                         }
  128.                         SetIoErr(err);
  129.  
  130.                         CloseLibrary((struct Library *)MMUBase);
  131.                 } else PrintFault(ERROR_OBJECT_NOT_FOUND,"MuFastChip requires the mmu.library");
  132.                 CloseLibrary((struct Library *)DOSBase);
  133.         }
  134.  
  135.         return rc;
  136. }
  137. ///
  138. /// ReadTTArgs
  139. struct RDArgs *ReadTTArgs(struct WBStartup *msg,LONG args[],struct RDArgs **tmp)
  140. {
  141. struct WBArg *wbarg;
  142. struct DiskObject *dop;
  143. char **tt;                      /* ToolTypes array */
  144. char *wbstr;                    /* Our self-made workbench argument string */
  145. char *here;
  146. BPTR oldlock;
  147. ULONG len;
  148. struct RDArgs *rd=NULL,*myrd=NULL;
  149. LONG err=0;
  150. BPTR newout;
  151.  
  152.         if (IconBase=OpenLibrary("icon.library",37L)) {
  153.                 if (wbarg=msg->sm_ArgList) {
  154.                         /* use a project icon if there is one... */
  155.                         if (msg->sm_NumArgs > 1) wbarg++;
  156.  
  157.                         /* go into the directory */
  158.                         oldlock=CurrentDir(wbarg->wa_Lock);
  159.  
  160.                         if (dop=GetDiskObject(wbarg->wa_Name)) {
  161.                                 if (tt=dop->do_ToolTypes) {
  162.                                         /* Read a special tool type for the output window */
  163.  
  164.                                         /* Calc the size of the argument string */
  165.  
  166.                                         len = 3;        /* reserve space for SPC,LF,NUL */
  167.                                         while (*tt) {
  168.                                                 len += strlen(*tt)+1;   /* string, plus space */
  169.                                                 tt++;
  170.                                         }
  171.  
  172.                                         if (wbstr=AllocVec(len,MEMF_PUBLIC)) {
  173.                                                 /* Now copy the arguments into this string, one by one
  174.                                                    and check whether the argument string is still valid. */
  175.  
  176.                                                 tt=dop->do_ToolTypes;
  177.                                                 here=wbstr;
  178.                                                 do{
  179.                                                         *here='\0';                     /* terminate string */
  180.                                                         /* Check whether this tool type is
  181.                                                            commented out. Just ignore it in this case */
  182.                                                         if (*tt) {
  183.                                                                 if (**tt=='(' || **tt==';')
  184.                                                                         continue;
  185.  
  186.                                                                 strcpy(here,*tt);      /* Add TT string */
  187.                                                         }
  188.                                                         len=strlen(here);
  189.                                                         here[len]='\n';
  190.                                                         here[len+1]='\0';               /* terminate string */
  191.  
  192.                                                         /* Now try to ReadArg' this string */
  193.  
  194.                                                         /* release old arguments left over from last loop */
  195.                                                         if (rd) FreeArgs(rd);
  196.                                                         if (myrd) FreeDosObject(DOS_RDARGS,myrd);
  197.                                                         rd=NULL;
  198.                                                         memset(args,0,sizeof(LONG)*OPT_COUNT);
  199.  
  200.                                                         if (myrd=AllocDosObject(DOS_RDARGS,NULL)) {
  201.                                                                 /* Allocate and setup the ReadArgs source */
  202.                                                                 myrd->RDA_Source.CS_Buffer=wbstr;
  203.                                                                 myrd->RDA_Source.CS_Length=strlen(wbstr);
  204.  
  205.                                                                 if (rd=ReadArgs(TEMPLATE ",WINDOW/K",args,myrd)) {
  206.                                                                         /* Is this still valid? */
  207.                                                                         here[len]=' ';
  208.                                                                         here+=len+1;
  209.                                                                         /* if so, accept this argument and go on */
  210.                                                                 } else {
  211.                                                                         err=IoErr();
  212.                                                                         if (err==ERROR_NO_FREE_STORE) break;
  213.                                                                         else    err=0;  /* Ignore unknown or invalid arguments silently */
  214.                                                                 }
  215.                                                         } else {
  216.                                                                 err=ERROR_NO_FREE_STORE;
  217.                                                                 break;
  218.                                                         }
  219.                                                 }while(*tt++);
  220.  
  221.                                                 FreeVec(wbstr);
  222.                                         } else err=ERROR_NO_FREE_STORE;
  223.                                 } else err=ERROR_REQUIRED_ARG_MISSING; /* Huh, how should this happen ? */
  224.                                 FreeDiskObject(dop);
  225.                         } else err=IoErr();
  226.                         CurrentDir(oldlock);
  227.                 } else err=ERROR_REQUIRED_ARG_MISSING; /* This should not happen either */
  228.                 CloseLibrary(IconBase);
  229.         } else err=ERROR_OBJECT_NOT_FOUND;    /* This should not happen */
  230.  
  231.         /* Open an output stream */
  232.  
  233.         if (err==0) {
  234.                 if (newout=Open((args[OPT_WINDOW])?((char *)args[OPT_WINDOW]):("NIL:"),MODE_NEWFILE)) {
  235.                         SelectOutput(newout);
  236.                         /* Hack in the output console. Well, well... */
  237.                         SetConsoleTask(((struct FileHandle *)(BADDR(newout)))->fh_Type);
  238.                 } else err=IoErr();
  239.         }
  240.  
  241.         if (err) {
  242.                 if (rd)   FreeArgs(rd);
  243.                 if (myrd) FreeDosObject(DOS_RDARGS,myrd);
  244.                 SetIoErr(err);
  245.                 rd=NULL;
  246.                 myrd=NULL;
  247.         }
  248.  
  249.         *tmp=myrd;
  250.         return rd;
  251. }
  252. ///
  253. /// EnableFastChip
  254. int EnableFastChip(void)
  255. {
  256. ULONG from,size;
  257.  
  258.         if (FindChipLimits(&from,&size)) {
  259.                 return SetCacheMode(from,size,MAPP_IMPRECISE|MAPP_NONSERIALIZED,MAPP_IMPRECISE|MAPP_NONSERIALIZED);
  260.         } else  return 20;
  261.  
  262. }
  263. ///
  264. /// DisableFastChip
  265. int DisableFastChip(void)
  266. {
  267. ULONG from,size;
  268.  
  269.         if (FindChipLimits(&from,&size)) {
  270.                 return SetCacheMode(from,size,0,MAPP_IMPRECISE|MAPP_NONSERIALIZED);
  271.         } else  return 20;
  272.  
  273. }
  274. ///
  275. /// FindChipLimits
  276. BOOL FindChipLimits(ULONG *from,ULONG *size)
  277. {
  278. struct MMUContext *ctx;
  279. ULONG psize;
  280. ULONG upper,lower;
  281. struct MemHeader *memh;
  282.  
  283.         ctx=DefaultContext();
  284.         psize=GetPageSize(ctx);
  285.         lower=0xffffffff;
  286.         upper=0;
  287.         Forbid();
  288.         for(memh=(struct MemHeader *)SysBase->MemList.lh_Head;
  289.             memh->mh_Node.ln_Succ;
  290.             memh=(struct MemHeader *)memh->mh_Node.ln_Succ) {
  291.  
  292.                 if (memh->mh_Attributes & MEMF_CHIP) {
  293.                         if ((ULONG)(memh->mh_Lower) < lower) {
  294.                                 lower=(ULONG)(memh->mh_Lower);
  295.                         }
  296.                         if ((ULONG)(memh->mh_Upper) > upper) {
  297.                                 upper=(ULONG)(memh->mh_Upper);
  298.                         }
  299.                 }
  300.         }
  301.         Permit();
  302.  
  303.         if (lower>=upper) {
  304.                 Printf("MuFastChip failed: no MEMF_CHIP memory found.\n");
  305.                 return FALSE;
  306.         }
  307.  
  308.         upper += psize-1;
  309.         lower &= -psize;
  310.         upper &= -psize;
  311.  
  312.  
  313.         *from=lower;
  314.         *size=upper-lower;
  315.  
  316.         return TRUE;
  317. }
  318. ///
  319. /// SetCacheMode
  320. int SetCacheMode(ULONG from,ULONG size,ULONG flags,ULONG mask)
  321. {
  322. struct MMUContext *ctx,*sctx;   /* default context, supervisorcontext */
  323. int err;
  324.  
  325.         ctx=DefaultContext();   /* get the default context */
  326.         sctx=SuperContext(ctx); /* get the supervisor context for this one */
  327.  
  328.         if (err=SetCache(ctx,from,size,flags,mask))
  329.                 return err;
  330.  
  331.         if (err=SetCache(sctx,from,size,flags,mask))
  332.                 return err;
  333.  
  334.         return 0;
  335. }
  336. ///
  337. /// SetCache
  338. int SetCache(struct MMUContext *ctx,ULONG from,ULONG size,ULONG flags,ULONG mask)
  339. {
  340. struct MinList *ctxl;
  341.  
  342.         if (ctxl=GetMapping(ctx)) {
  343.          if (SetProperties(ctx,flags,mask,from,size,TAG_DONE)) {
  344.           if (RebuildTree(ctx)) {
  345.                   ReleaseMapping(ctx,ctxl);
  346.                   return 0;     /* Everything worked fine */
  347.           } /* the MMU tree could not be build. It is unmodified in this case */
  348.          }
  349.          /* Re-install the old properties. This can't fail. */
  350.  
  351.          SetPropertyList(ctx,ctxl);
  352.          ReleaseMapping(ctx,ctxl);
  353.         }
  354.  
  355.         return ERROR_NO_FREE_STORE;
  356. }
  357. ///
  358.  
  359.